package com.shuyu.web.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.shuyu.common.core.controller.BaseController;
import com.shuyu.common.core.domain.Result;
import com.shuyu.common.core.domain.entity.Customer;
import com.shuyu.common.utils.DesCode;
import com.shuyu.domain.Order;
import com.shuyu.service.CustomerService;
import com.shuyu.service.OrderService;
import com.shuyu.vo.DayouOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/dayou")
@Api(tags = "大友相关")
public class DaYouController extends BaseController {
    @Resource
    private OrderService orderService;
    @Resource
    private CustomerService customerService;
    @GetMapping("/getUserOrder")
    @ApiOperation("根据手机号查询未兑换道具订单列表")
    public Result<List<DayouOrderVo>> getUserOrder(String mobile) {
        Customer customer = customerService.getOne(new LambdaQueryWrapper<Customer>().eq(Customer::getMobile, DesCode.encrypt(mobile)));
        if(customer == null){
            return Result.success();
        }
        List<Order> list = orderService.list(new LambdaQueryWrapper<Order>().eq(Order::getStatus,1).eq(Order::getSource,1)
                .eq(Order::getExchangeStatus,0).eq(Order::getCustomerId,customer.getId()));
        List<DayouOrderVo> result = new ArrayList<>();
        for (Order order : list) {
            DayouOrderVo dayouOrderVo = new DayouOrderVo(mobile, order.getOrderSn(), order.getPropId(), order.getCount());
            result.add(dayouOrderVo);
        }
        return Result.success(result);

    }
    @PostMapping("/updateOrderStatus")
    @ApiOperation("更新订单号")
    public Result updateOrderStatus(String orderSn){
        return Result.success(orderService.update(new LambdaUpdateWrapper<Order>().set(Order::getExchangeStatus,1).eq(Order::getOrderSn,orderSn)));
    }

}
